﻿using System;
using System.Collections.Generic;
using ParsePtb.consts;

namespace ParsePtb.sign
{
    public class MySign
    {

        //是否忽略
        public bool Igore { get; set; }

        //是否关联供应商
        public bool HaveSupplier { get; set; }

        //查询的条件  如 设置 Project_NO
        public string WhereCon { get; set; }

        //忽略的字段 (线上不存在)
        public Dictionary<string, string> IgoreFields { get; set; }

        //存在的字段 （已Fields为主 没有值 已 IgoreFields为过滤)
        public Dictionary<string, string> Fields { get; set; }

        //表的别名  两端数据库名称不一致
        public string TableAlias { get; set; }


        //走内置的自定义函数处理
        public bool DefinedHander { get; set; }


        // to_timestamp('{currentValue}','yyyy-mm-dd hh24:mi:ss')
        //需要格式化时间的字段 通用
        public Dictionary<string, string> FormatData { get; set; }

        //public string Aa { get => aa; set => aa = value; }

        public MySign()
        {
            Igore = false;//默认该表要操作
            HaveSupplier = true;//默认关联
            IgoreFields = new Dictionary<string, string>();
            DefinedHander = false;//默认走公共方法

            this.FormatData = new Dictionary<string, string>();//格式化

            // 通用格式化字段
            this.FormatData.Add("CREATE_TIME", Constant.FORMAT_DATE_TIMESTAMP);
            this.FormatData.Add("UPDATE_TIME", Constant.FORMAT_DATE_TIMESTAMP);
        }

        /// <summary>
        /// 设置数据库所有字段
        /// </summary>
        /// <param name="fields"></param>
        public MySign(string fields) : this()
        {
            CheckEmpty(fields);
            string[] fieldArr = fields.Split(',');
            this.Fields = new Dictionary<string, string>();

            foreach (var item in fieldArr)
            {
                if (!this.Fields.ContainsKey(item.Trim()))
                {
                    this.Fields.Add(item.Trim(), item.Trim());
                }
            }

        }

        private void CheckEmpty(string fields)
        {
            //内容为空
            if (string.IsNullOrWhiteSpace(fields))
            {
                throw new Exception("fields is empty or null,please you check the content");
            }
        }

        /// <summary>
        /// 设置别名
        /// </summary>
        /// <param name="tableAlias"></param>
        /// <returns></returns>
        public MySign Alias(string tableAlias)
        {
            this.TableAlias = tableAlias;
            return this;
        }


        /// <summary>
        /// 设置数据库过滤字段i
        /// </summary>
        /// <param name="tableAlias"></param>
        /// <returns></returns>
        public MySign Filter(string igoreFields)
        {
            if (this.IgoreFields == null)
            {
                this.IgoreFields = new Dictionary<string, string>();
            }
            if (string.IsNullOrEmpty(igoreFields))
            {
                return this;
            }

            string[] fieldArr = igoreFields.Split(',', '，');

            foreach (var item in fieldArr)
            {
                if (!this.IgoreFields.ContainsKey(item.Trim()))
                {
                    this.IgoreFields.Add(item.Trim(), item.Trim());
                }
            }
            return this;
        }

        /// <summary>
        /// 设置数据库所有填充数据的字段
        /// </summary>
        /// <param name="tableAlias"></param>
        /// <returns></returns>
        public MySign Colums(string fields)
        {
            if (string.IsNullOrEmpty(fields))
            {
                return this;
            }

            string[] fieldArr = fields.Split(',', '，');
            this.Fields = new Dictionary<string, string>();
            foreach (var item in fieldArr)
            {
                if (!this.Fields.ContainsKey(item.Trim()))
                {
                    this.Fields.Add(item.Trim(), item.Trim());
                }
            }
            return this;
        }

        /// <summary>
        ///  追加特殊的内容格式处理  如日期  等等
        /// </summary>
        /// <param name="key"></param>
        /// <param name="formatValue"></param>
        /// <returns></returns>
        public MySign AppendFormat(string key, string formatValue)
        {
            if (this.FormatData.ContainsKey(key))
            {
                this.FormatData.Remove(key);
            }
            this.FormatData.Add(key, formatValue);
            return this;
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public MySign Where(string where)
        {
            this.WhereCon = where;
            return this;
        }
    }
}
